package com.suguowen.sblog.controller;

import com.suguowen.sblog.bean.Tag;
import com.suguowen.sblog.bean.User;
import com.suguowen.sblog.bean.vo.ArticledetailVO;
import com.suguowen.sblog.bean.vo.CommonVO;
import com.suguowen.sblog.common.MySpringContext;
import com.suguowen.sblog.service.ArticleService;
import com.suguowen.sblog.service.DiscussionService;
import com.suguowen.sblog.service.HomeService;
import com.suguowen.sblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    HomeService show;
    @Autowired
    TagService tagService;
    @Autowired
    DiscussionService discussionService;
    /**
     * title
     * content
     * create_time
     * author
     */
    // 查看文章
    @RequestMapping("/toArticle/{articleId}")
    public String showArticle(@PathVariable("articleId")int articleId,
                              Model model, HttpSession httpSession){
        // 是否登录
        User user = MySpringContext.getInstance().getUser(httpSession.getId());
        model.addAttribute("user",user);
        // 获得文章详情
        ArticledetailVO vo =  articleService.readArticle(articleId);
        // 获得tag栏
        Tag tag = tagService.findTagById(articleId);
        if (tag != null){
            model.addAttribute("currentTag",tag.getId());
            model.addAttribute("currentTagName",tag.getName());
        }
        // 获取评论区信息
        List<CommonVO> commonVOList =  discussionService.showCommon(articleId);
        model.addAttribute("commonVOList",commonVOList);

        model.addAttribute("tags", tagService.findAllTag());
        model.addAttribute("articledetail",vo);
        return "article";
    }

    // 写文章
    @RequestMapping("/createArticle")
    public String createArticle(Model model){
        model.addAttribute("tags", tagService.findAllTag());
        return "simpleEditor";
    }

    // 修改文章
    @RequestMapping("/modifyArticle/{articleId}")
    public String modefyArticle(@PathVariable("articleId") int articleId,
                                Model model){
        // 通过id获得文章内容、标题
        // 渲染文章
        Map<String,Object> map = articleService.modifyArt(articleId);
        model.addAttribute("id",articleId);
        model.addAttribute("title",map.get("title"));
        model.addAttribute("content",map.get("content"));
        model.addAttribute("tags", tagService.findAllTag());
        model.addAttribute("currentTagId",map.get("tagId"));
        return "simpleEditor";
    }
    // 修改完成提交
    @RequestMapping("/submit")
    @ResponseBody
    public void submitModifyArticle(@RequestParam("articleId") Integer articleId,
                                      @RequestParam(value = "title")String title,
                                      @RequestParam(value = "html")String html,
                                      @RequestParam(value = "markdown")String markdown,
                                      @RequestParam(value = "tagId")Integer tagId
                                    ){
        // 更新article、article_detail
        Date date = new Date();
        Timestamp nowtime = new Timestamp(date.getTime());
        Integer userId = 1001;// 临时值，用户功能未完善、应从session获取
        // 判断id是否存在
        if (articleId == null){
            articleService.createArt(title,html,markdown,nowtime,tagId,userId);
        } else{
            articleService.saveArt(articleId,title,html,markdown,nowtime,tagId);
        }

    }

    // 删除文章
    @RequestMapping("/deleArticle/{articleId}")
    public String deleArticle(@PathVariable("articleId") int articleId){
        // 删除article、article_detail
        articleService.deleArt(articleId);
        return "redirect:/home";
    }
}
